package cn.kgc.web.controller;

import cn.kgc.base.entity.City;
import cn.kgc.base.utils.ResponseResult;
import cn.kgc.web.service.CityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/1/11
 * @description
 */
@RestController
@RequestMapping("city")
public class CityController {

    @Resource
    private CityService cityService;

    //  城市列表
    @GetMapping
    public ResponseResult cityList() {

        List<City> list = cityService.list();
        return ResponseResult.success().data(list);
    }


    @GetMapping("{id}")
    public City getCityById(@PathVariable("id") Integer id) {
        City city = cityService.getById(id);
        return city;
    }

    // 添加城市
    @PostMapping
    public ResponseResult addCity(@RequestBody City city) {

        // id 是自增的id  添加完毕之后  会将自增的id自动设置在city中
        cityService.save(city);

        return ResponseResult.success().data(city).message("添加成功");
    }

}
